<?php

namespace app\index\controller;

use app\index\controller\Base;
use app\admin\controller\Nature;
use app\index\model\Course;
use app\index\model\User;
use app\index\model\Apply;
use app\index\model\Naturetype;
use app\index\model\Text;
use think\Request;
use think\File;
use think\Session;

class  Index extends Base
{
    public function gzh()
    {
        $data = array(
            "text" => $this->getAllText(),
        );
        return view("gzh", $data);
    }
    //上传文件
    public function upload(Request $request)
    {
        $user = $this->user_info();
        //接收上传的文件
        $file = $this->request->file('file');
        if (!empty($file)) {
            //图片存的路径
            $imgUrl = ROOT_PATH . 'public' . DS . 'static' . DS . 'admin' . DS . 'uploads' . DS . 'useridcard';
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['size' => 1048576, 'ext' => 'jpg,png,gif'])->rule('uniqid')->move($imgUrl);
            $error = $file->getError();
            //验证文件后缀后大小
            if (!empty($error)) {
                dump($error);
                exit;
            }
            if ($info) {
                // 成功上传后 获取上传信息

                //获取图片的名字
                $imgName = $info->getFilename();
                //获取图片的路径
                $photo = $imgName;

            } else {
                // 上传失败获取错误信息
                $file->getError();
            }
        } else {
            $photo = '';
        }
        if ($photo !== '') {
            return ['code' => 1, 'msg' => '成功', 'photo' => $photo];
        } else {
            return ['code' => 404, 'msg' => '失败'];
        }
    }

    //添加
    public function add(Request $request)
    {
        //##准备数据
        $data = $request->post();
        //判断报名端口
        if ($data['applytype'] == "zx") {
            $url = "Index/index1";
        } elseif ($data['applytype'] == "ld") {
            $url = "Index/index";
        }
        //判断是否要交费
        $time = time();
        $pwd = md5($this->getSetting('defaultPwd')['val']);
        //查询课程名称
        $course = Course::where(array('id' => $data['courseid']))->find();
        //查询性质
        $natrue = Naturetype::where(array('id' => $data['natrueid']))->find();
        //在user表中查询当前用户
        $userRes = User::where(array("idcard" => $data['idcard']))->find();
        //如果该用户已经注册则无需注册，如果没有则需要注册
        if (!$userRes) {
            $Userdata = Array(
                "name" => $data['username'],
                "pwd" => $pwd,
                "sex" => $data['sex'],
                "phone" => $data['phone'],
                "idcard" => $data['idcard'],
                "type" => 1, //1新报名
                "province" => $data['province'],
                "city" => $data['city'],
                "district" => $data['district'],
                "detailmap" => $data['detailmap'],
                "create_time" => $time,
                "update_time" => $time,
            );
            User::insert($Userdata);
        } else {
            $Userdata = Array(
                "sex" => $data['sex'],
                "phone" => $data['phone'],
                "province" => $data['province'],
                "city" => $data['city'],
                "district" => $data['district'],
                "detailmap" => $data['detailmap'],
                "update_time" => $time,
            );
            User::where('id=' . $userRes['id'])->update($Userdata);
        }
        //判断是否已已经注册的个人信息是否相同
//            else{
//                $applyData = array(
//                    "name" => $data['username'],
//                    "sex" => $data['sex'],
//                    "phone" => $data['phone'],
//                    "idcard" => $data['idcard'],
//                    "province" => $data['province'],
//                    "city" => $data['city'],
//                    "district" => $data['district'],
//                    "detailmap" => $data['detailmap'],
//                );
//                $userRess = User::where($applyData)->find();
//                if(!$userRess){
//                    $this->error("个人信息有误，请与首次报名或账户信息一致。");
//                }
//            }

        //##报名表数据
        //查询当前用户在当前报名表中是否已经报过名
        $applyRes = Apply::where(array("idcard" => $data['idcard'], "courseid" => $data['courseid']))->find();
        if (!$applyRes) {
            $applyData = array(
                "username" => $data['username'],
                "sex" => $data['sex'],
                "phone" => $data['phone'],
                "idcard" => $data['idcard'],
                "province" => $data['province'],
                "city" => $data['city'],
                "district" => $data['district'],
                "detailmap" => $data['detailmap'],
                "natrueid" => $data['natrueid'],
                "natrue" => $natrue['type_name'],
                "courseid" => $data['courseid'],
                "course" => $course['name'],
                "comment" => $data['comment'],
                "status" => 1, //1审核中，2通过，3拒绝
                "create_time" => $time,
                "update_time" => $time,
            );
            $res = Apply::insert($applyData);
            if ($res) {
                $this->success("提交成功，请耐心等待审核...", $url);
            } else {
                $this->error("提交失败", $url);
            }
        } else {
            $this->error("报名失败，您已经报名过该课程。", $url);
        }

    }



}
